/**
 * 表单填报 列表页
 *
 * 作者 zhangwei
 * 日期 2024-09-05 08:53:07
 */
layui.use(['jquery', 'laydate', 'febs', 'form', 'table', 'commonJS', 'validate'], function () {
    var $ = layui.jquery,
        laydate = layui.laydate,
        febs = layui.febs,
        form = layui.form,
        table = layui.table,
        commonJS = layui.commonJS,
        validate = layui.validate,
        $view = $('#febs-formFill'),
        $query = $view.find('#query'),
        $searchForm = $view.find('#formFill-table-form'),
        tableIns;

    form.verify(validate);
    form.render();
    initDate()
    function initDate() {
        //初始化日期控件
        laydate.render({
            elem: '#laydate-range',
            range: ['#startDate', '#endDate'],
            format: 'yyyy-MM-dd',
            btns: ['now', 'confirm']
        })
        // $('#endDate').val(commonJS.getCurrentDateStr(new Date()));
        // $('#startDate').val(commonJS.getPastDateStr(new Date(), 7));
        form.render();

    }
    initTable();
    function initTable() {
        tableIns = febs.table.init({
            elem: $view.find('table'),
            id: 'formFillTable',
            url: ctx + 'formDesignFill/list',
            where: getQueryParams(),
            height: getTableHeight(),
            skin: '',
            cols: [[
                {type: 'numbers', title: '序号'},
                {field: 'tenantName', title: '公司名称', minWidth: 260,hide:!hasHeadCompanyRole},
                {field: 'formName', title: '表单名称', minWidth: 120,event:'detail',templet:function (d) {
                        return '<p style="color:#01aaed;cursor:pointer">'+d.formName+'</p>'
                    }},
                {field: 'startDate', title: '开始时间', width: 120},
                {field: 'endDate', title: '结束时间', width: 120},
                {title: '操作', toolbar: '#formFill-tool', align: 'center', width: 100}
            ]],
            // toolbar: '#formFill-toolbar',
            defaultToolbar: []
        });
    }

    table.on('sort(formFillTable)', function (obj) {
        tableIns.reload({
            initSort: obj,
            where: $.extend(getQueryParams(), {
                field: obj.field,
                order: obj.type
            }),
            autoSort: true
        });
    });

    function reloadPage(curr) {
        var _curr = $view.find(".layui-laypage-em").next().html();
        if (!!curr) {
            _curr = curr;
        }
        tableIns.reload({where: getQueryParams(), page: {curr: _curr}});
    }

    $query.on('click', function () {
        reloadPage(1);
    });

    table.on('tool(formFillTable)', function (obj) {
        var data = obj.data,
        layEvent = obj.event;
        let url = '',type=1
        if(layEvent === 'detail'){
            if(!!data.fillHtml){
                url = 'formFill/update/' + data.registerId
                type=2
            }else{
                url = 'formFill/add/' + data.designId
                type=1
            }
        }
        if (layEvent === 'edit' || layEvent === 'add') {
            if(layEvent === 'edit'){
                url = 'formFill/update/' + data.registerId
                type=2
            }else{
                url = 'formFill/add/' + data.designId
                type=1
            }
        }
        febs.modal.open('表单录入', url, {
            area: [$(window).width() <= 750 ? '95%':'50%','95%'],
            btn: ['保存','提交', '取消'],
            data:{
                type:type
            },
            yes: function (index, layero) {
                window.localStorage.setItem('submitFlg','1')
                $('#formFill-edit').find('#formDemoSubmit').trigger('click');
            },
            btn2: function () {
                window.localStorage.setItem('submitFlg','2')
                $('#formFill-edit').find('#formDemoSubmit').trigger('click');
                return false;
            },
            btn3: function () {
                layer.closeAll();
            }
        });
    });

    function getQueryParams() {
        return {
            formName: $searchForm.find('input[name="formName"]').val()?.trim(),
            tenantId: $searchForm.find("select[name='tenantId']").find("option:selected").val(),
            startDate: $searchForm.find('input[name="startDate"]').val(),
            endDate: $searchForm.find('input[name="endDate"]').val(),
            status:2,
            invalidate_ie_cache: new Date()
        };
    }

    function getTableHeight() {
        return commonJS.calcTableHeight();
    }

    $(window).resize(function () {
        tableIns.reload({height: getTableHeight()});
    });

});